package com.bw.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.aop.MyLog;
import com.bw.common.Result;
import com.bw.common.UploadPicUtils;
import com.bw.config.SecurityConfig;
import com.bw.entity.*;
import com.bw.form.UserQueryForm;
import com.bw.mapper.SysDeptMapper;
import com.bw.mapper.SysRoleMapper;
import com.bw.mapper.SysUserMapper;
import com.bw.mapper.SysUserRoleMapper;
import com.bw.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.HttpMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户接口
 */
@Api(tags = {"用户接口"})
@RestController
public class UserController {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysDeptMapper sysDeptMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    UserService userService;


    /**
     * 用户列表
     */
    @RequestMapping("/getUserPage")
    @ApiOperation(value = "用户接口-分页列表",httpMethod = "GET")
    @MyLog(ops = "用户列表")
    public IPage<SysUser> getUserPage(Page page, UserQueryForm query) {

        return userService.getUserPage(page,query);

    }

    /**
     * 部门下拉框
     */
    @ApiOperation(value = "用户接口-部门下拉框",httpMethod = "GET")
    @RequestMapping("/getDeptList")
    public List<SysDept> getDeptList() {
        return sysDeptMapper.selectList(new QueryWrapper<>());
    }

    /**
     * 上传头像
     *
     * @param file
     * @param request
     * @return
     */
    @ApiOperation(value = "用户接口-上传头像",httpMethod = "POST")
    @RequestMapping("/uploadAvatar")
    public Result uploadAvatar(MultipartFile file, HttpServletRequest request) {
        String url = UploadPicUtils.upload(file, request);

        return Result.ok(url);
    }

    /**
     * 查询用户角色
     *
     * @return
     */
    @RequestMapping("/getRoleByUser")
    @ApiOperation(value = "用户接口-查询用户角色",httpMethod = "GET")
    public Result getRoleByUser(Long userId) {

        // 获取当前用户的角色
        List<SysUserRole> list = sysUserRoleMapper.selectList(Wrappers.lambdaQuery(SysUserRole.class).eq(SysUserRole::getUserId, userId));
        List<Long> collect = list.stream().map(sysUserRole -> sysUserRole.getRoleId()).collect(Collectors.toList());
        // 获取所有的角色列表
        List<SysRole> roleList = sysRoleMapper.selectList(Wrappers.lambdaQuery(SysRole.class).eq(SysRole::getDelFlag, 0));
        roleList.stream().forEach(sysRole -> {
            if (collect.contains(sysRole.getId())) {
                sysRole.setChecked(1);
            } else {
                sysRole.setChecked(0);
            }
        });
        return Result.ok(roleList);
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("/addUser")
    @ApiOperation(value = "用户接口-添加用户",httpMethod = "POST")
    public Result addUser(@RequestBody SysUser sysUser) {
        userService.addUser(sysUser);
        return Result.ok("success");
    }

    /**
     * 修改用户
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("/updateUser")
    @ApiOperation(value = "用户接口-修改用户",httpMethod = "POST")
    public Result updateUser(@RequestBody SysUser sysUser) {
        userService.updateUser(sysUser);
        return Result.ok("success");
    }
    /**
     * 删除用户
     *
     * @return
     */
    @RequestMapping("/deleteUser/{ids}")
    @ApiOperation(value = "用户接口-删除用户",httpMethod = "DELETE")
    public Result deleteUser(@PathVariable String ids) {
        userService.deleteUser(ids);
        return Result.ok("success");
    }

    /**
     * 修改用户角色
     *
     * @param userRoleDto
     * @return
     */
    @RequestMapping("/saveUserRole")
    @ApiOperation(value = "用户接口-分配用户角色",httpMethod = "POST")
    public Result saveUserRole(@RequestBody UserRoleDto userRoleDto) {
        userService.saveUserRole(userRoleDto);
        return Result.ok("success");
    }
}
